%% Housekeeping
clear
close all
clc

%% Add paths
addpath(genpath('Fig_I'))


%% Output folder for figures
Figures_path=strcat('.\Figures');

%% Graphic Settings
set(0,'defaultlinelinewidth',2);
set(0,'defaultaxesfontsize',10);  %12
set(0,'defaulttextfontsize',10);  %12
set(0,'defaultlinelinewidth',2);

rrs = get(0,'screensize');
rrf = get(0,'defaultfigurepos');

defpos = [00, 00, 1200, 300]; %2100 700
set(0,'defaultfigurepos',defpos);

%% Parameters and Settings
phiM=2;           % monetary pol response to infl Monetary Dominance regime
phiF=0;           % monetary pol response to infl Fiscal Dominance regime
gamM=20;          % primary surplus adjustment to debt Monetary Dominance regime
gamF=0;           % primary surplus adjustment to debt Fiscal Dominance regime

param(1)=phiM;     % monetary pol response to infl
param(2)=gamM;     % primary surplus adjustment to debt
param(3)=0.5;      % rho_tau persistence of surplus shock
param(4)=1/.99;    % rstar long run interest rate
param(5)=1/(param(4)*(1-1/param(4)))/1.96; %fiscal shock rescaled to produce a unit response of inflation to unfunded transfers

paramSS=param;
paramSS(6)=phiF; 
paramSS(7)=gamF; 

nirf=20;
dofig=0;            %dofig=1, the code shows and saves the graph (Figure_I.pdf in teh subfolder .\Figures)
bw=0;               %black and white graphs

%% Constructing the matrices and Solving the Model

%Monetary Led
[GAM0,GAM1,PSI,PPI] = gammas_R(param);

[GGm,Cm,RRm,~,~,~,~,eum,~]=gensys(GAM0,GAM1,zeros(size(GAM0,1),1),PSI,PPI);

%Fiscal Led
param(1)=phiF; param(2)=gamF;
[GAM0,GAM1,PSI,PPI] = gammas_R(param);

[GGf,Cf,RRf,~,~,~,~,euf,~]=gensys(GAM0,GAM1,zeros(size(GAM0,1),1),PSI,PPI);

%Shock-Specific Rule
[GAM0,GAM1,PSI,PPI] = gammas_ShockSpecific_R(paramSS);

[GGss,Css,RRss,~,~,~,~,euss]=gensys(GAM0,GAM1,zeros(size(GAM0,1),1),PSI,PPI);

if eum(1)==1 && eum(2)==1 && euf(1)==1 && euf(2)==1 && euss(1)==1 && euss(2)==1
    disp('DETERMINACY')
%% IRF of Funded Fiscal Shocks
    indshock=1; indshock2=1;
    ssirm=zeros(size(GGm,1),nirf); ssirf=zeros(size(GGf,1),nirf); ssirss=zeros(size(GGss,1),nirf);
    ssirm(:,1)=-RRm(:,indshock); ssirf(:,1)=-RRf(:,indshock); ssirss(:,1)=-RRss(:,indshock2);
    for t=2:nirf
        ssirm(:,t)=GGm*ssirm(:,t-1);
        ssirf(:,t)=GGf*ssirf(:,t-1);
        ssirss(:,t)=GGss*ssirss(:,t-1);
    end
    for i=1:size(GGm,1)
        ssirm(i,find(abs(squeeze(ssirm(i,:,1)))<1e-8),1)=0;
        ssirf(i,find(abs(squeeze(ssirf(i,:,1)))<1e-8),1)=0;
    end
    for i=1:size(GGss,1)
        ssirss(i,find(abs(squeeze(ssirss(i,:,1)))<1e-8),1)=0;
    end
   
    
    %% IRF of Unfunded Fiscal Shocks
    
    indshock=1; indshock2=2;
   
    ssirm(:,1,2)=-RRm(:,indshock); ssirf(:,1,2)=-RRf(:,indshock); ssirss(:,1,2)=-RRss(:,indshock2);
    for t=2:nirf
       ssirm(:,t,2)=GGm*ssirm(:,t-1,2);
       ssirf(:,t,2)=GGf*ssirf(:,t-1,2);
       ssirss(:,t,2)=GGss*ssirss(:,t-1,2);
    end  
    
    for i=1:size(GGm,1)
        ssirm(i,find(abs(squeeze(ssirm(i,:,2)))<1e-8),2)=0;
        ssirf(i,find(abs(squeeze(ssirf(i,:,2)))<1e-8),2)=0;
    end
    for i=1:size(GGss,1)
        ssirss(i,find(abs(squeeze(ssirss(i,:,2)))<1e-8),2)=0;
    end
    
    
    %%
    
    figure
    subplot(1,3,1)
    plot(1:10,ssirm(2,1:10,1),'b','markersize',8)
    grid on
    axis tight
    ylim([0,1]);
    title('Always Monetary-led')
    ylabel('Inflation', 'FontWeight', 'bold')
    
    
    subplot(1,3,2)
    plot(1:10,ssirf(2,1:10,1),'--r','markersize',8)
    grid on
    axis tight
    title('Always Fiscally-led')
    
    subplot(1,3,3)
    plot(1:10,ssirss(2,1:10,1),'b','markersize',8)
    hold on
    plot(1:10,ssirss(2,1:10,2),'--r','markersize',8)
    grid on
    hold off
    axis tight
    title('Partially Unfunded Debt')
    h1=legend('Funded fiscal shock','Unfunded fiscal shock');
    tightfig; 
    savefigure_pdf(strcat(Figures_path,'\Figure_I'));
    
    
else
    disp('INDETERMINACY - Multiple Stable REE Solutions OR NO STABLE REE SOLUTIONS')
end




